package com.wenbing.miio.mi;

import com.wenbing.miio.core.MacAddress;
import com.wenbing.miio.core.NetworkLayer;
import com.wenbing.miio.core.NetworkStack;
import com.wenbing.miio.core.Device;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

/**
 * @author wen bing
 * @projectName: miio
 * @package: com.wenbing.miio.util
 * @className: DeviceUtil
 * @date 2024/09/19 16:33
 **/
public class MiDeviceUtil {

    public static final String MI_DID = "mi_did";

    public static final String MI_TOKEN = "mi_token";

    public static int getDeviceId(Device.HardwareConfiguration hardware) {
        return hardware.getAttribute(MI_DID);
    }

    public static int getDeviceId(NetworkLayer layer) {
        NetworkStack networkStack = layer.getNetworkLayerNode()
                .getNetworkStack();
        return getDeviceId(networkStack.getConfiguration());
    }

    public static byte[] getToken(Device.HardwareConfiguration hardware) {
        return hardware.getAttribute(MI_TOKEN);
    }

    public static byte[] getToken(NetworkLayer layer) {
        NetworkStack networkStack = layer.getNetworkLayerNode()
                .getNetworkStack();
        Device.HardwareConfiguration configuration = networkStack.getConfiguration();
        return getToken(configuration);
    }

    public static long getRunningTime(NetworkLayer layer) {
        NetworkStack networkStack = layer.getNetworkLayerNode()
                .getNetworkStack();
        long time = networkStack.getClock().millis();
        return TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS);
    }
}
